﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class ReceiveNoteControl : UserControl
    {
        Product prod = new Product();
        Partner partner = new Partner();
        Users users = new Users();
        Users ActiveUser = Program.GetActiveUser;
        Int32 Import;
        DataTable table = new DataTable();

        public ReceiveNoteControl()
        {
            Program.GetMainForm.Text = "TMS - Приемници";
            InitializeComponent();
            table = prod.GetAllReceiveNotes().Tables[0];
            dgvReceiveNoteList.DataSource = table;
            dgvReceiveNoteList.Columns[0].Visible = false;
            dgvReceiveNoteList.Columns[1].HeaderText = "Датум";
            dgvReceiveNoteList.Columns[2].HeaderText = "Одговорен";
            dgvReceiveNoteList.Columns[3].HeaderText = "Приемница бр.";
            dgvReceiveNoteList.Columns[3].DisplayIndex = 1;
            dgvReceiveNoteList.Columns[4].HeaderText = "Коминтент";
            dgvReceiveNoteList.Columns[5].HeaderText = "Статус";

            //foreach (DataGridViewRow NoteRow in dgvReceiveNoteList.Rows)
            //{

            //    string name = partner.GetPartner(Convert.ToInt32(NoteRow.Cells[4].Value)).Rows[0].ItemArray[0].ToString();
            //   // MessageBox.Show(partner.GetPartner(Convert.ToInt32(NoteRow.Cells[4].Value)).Rows[0].ItemArray[0].ToString());

            //    dgvReceiveNoteList.Rows[i].Cells[4].ValueType = typeof(string);
            //    dgvReceiveNoteList.Rows[i].Cells[4].Value = name;
            //    i++;
            //}

            FillResponsibles();
            FillPartners();
        }

        public ReceiveNoteControl(DataGridViewRow row)
        {
            InitializeComponent();
            cmbValute.SelectedItem = "MKD";
            table = prod.GetAllReceiveNotes().Tables[0];
            dgvReceiveNoteList.DataSource = table;
            if (row.Cells[0].Value.ToString() == "0")
            {
                Program.GetMainForm.Text = "TMS - Приемници";
                gbImport.Visible = false;
                dgvReceiveNoteList.Columns[6].Visible = false;
                dgvReceiveNoteList.Columns[7].Visible = false;
                dgvReceiveNoteList.Columns[8].Visible = false;
                dgvReceiveNoteList.Columns[9].Visible = false;
                dgvReceiveNoteList.Columns[10].Visible = false;
                dgvReceiveNoteList.Columns[11].Visible = false;
                table.DefaultView.RowFilter = string.Format("Import =0");

                Import = 0;
            }
            else
            {
                Program.GetMainForm.Text = "TMS - Увозни приемници";
                dgvReceiveNoteList.Columns[6].Visible = false;
                dgvReceiveNoteList.Columns[7].HeaderText = "Транспорт";
                dgvReceiveNoteList.Columns[8].HeaderText = "Шпедиција";
                dgvReceiveNoteList.Columns[9].HeaderText = "Складишнина";
                dgvReceiveNoteList.Columns[10].HeaderText = "Валута";
                dgvReceiveNoteList.Columns[11].HeaderText = "Курс";
                table.DefaultView.RowFilter = string.Format("Import =1");
                Import = 1;
            }
                dgvReceiveNoteList.Columns[0].Visible = false;
            dgvReceiveNoteList.Columns[1].HeaderText = "Датум";
            dgvReceiveNoteList.Columns[2].HeaderText = "Одговорен";
            dgvReceiveNoteList.Columns[3].HeaderText = "Приемница бр.";
            dgvReceiveNoteList.Columns[3].DisplayIndex = 1;
            dgvReceiveNoteList.Columns[4].HeaderText = "Коминтент";
            dgvReceiveNoteList.Columns[5].HeaderText = "Статус";
            dgvReceiveNoteList.Columns[12].HeaderText = "Фактура бр.";

            if (row.Cells.Count == 2)
            {
                Program.GetMainForm.Text = "TMS - Повратници";
                txtPartnerNoteNumber.Enabled = false;
                txtPartnerNoteNumber.Text = "Повратница";
                table.DefaultView.RowFilter = string.Format("InvoiceNo ='Повратница'");
                dgvReceiveNoteList.Update();
                //gbGeneral.Visible = true;
                //grNew.Visible = true;
            }
            

            FillResponsibles();
            FillPartners();
            dgvReceiveNoteList.Update();
            dgvReceiveNoteList.FirstDisplayedScrollingRowIndex = dgvReceiveNoteList.RowCount - 1;
        }

        public void FillResponsibles()
        {
            DataTable table = users.GetAllUserNameAndSurname();
            foreach (DataRow row in table.Rows)
            {
                cmbResponsible.Items.Add(row.ItemArray[0].ToString());
                cmbResponsible.Items.Remove("Администратор ");
                cmbResponsible.SelectedItem = ActiveUser.GetName()+" "+ActiveUser.GetSurname();
            }
        }

        public void FillPartners() {

            cmbPartners.DataSource = partner.GetAllPartners();
            cmbPartners.DisplayMember = "name";
            cmbPartners.ValueMember = "id";

        }

      

        private void dgvDispatchList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_RECEIVE_NOTE_CONTROL, dgvReceiveNoteList.CurrentRow);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_RECEIVE_NOTE_CONTROL, dgvReceiveNoteList.CurrentRow);
            this.Dispose();
        }

        

      

        private void ToStClose_Click(object sender, EventArgs e)
        {
            if (dgvReceiveNoteList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано испратница");
            else
            {
                if (dgvReceiveNoteList.SelectedRows[0].Cells[5].Value.ToString() == "Затворена")
                    MessageBox.Show("Испратницата е веќе затворена");
                else
                {
                    if (Import == 0)
                        prod.CloseReceiveNote(Convert.ToInt32(dgvReceiveNoteList.SelectedRows[0].Cells[3].Value));
                    else
                        prod.CloseImportReceiveNote(Convert.ToInt32(dgvReceiveNoteList.SelectedRows[0].Cells[3].Value));
                    this.Dispose();
                    DataGridView tmp = new DataGridView();
                    tmp.RowCount = 1;
                    tmp.ColumnCount = 1;
                    tmp.Rows[0].Cells[0].Value = Import;
                    Program.GetMainForm.ShowControl(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
                }
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            grNew.Visible = true;
        }

        

        private void ToStSave_Click(object sender, EventArgs e)
        {
            prod.AddReceiveNote(prod.GetLastReceiveNote() + 1, cmbResponsible.SelectedItem.ToString(), dtpDate.Value, partner.GetPartner(Convert.ToInt32(cmbPartners.SelectedValue)).Rows[0].ItemArray[0].ToString(), "Отворена", Import, txtTransport.Text, txtSpedition.Text, txtWarehousing.Text, cmbValute.SelectedItem.ToString(), txtCurse.Text, txtPartnerNoteNumber.Text);
            this.Dispose();
            DataGridView tmp = new DataGridView();
            tmp.RowCount = 1;
            tmp.ColumnCount = 1;
            tmp.Rows[0].Cells[0].Value = Import;
            Program.GetMainForm.ShowControl(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
        }

       

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

        
        
    }
}
